//
//  InputNameView.swift
//  OnemeetingDemo
//
//  Created by zhangheng on 2020/2/10.
//  Copyright © 2020 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

class InputNameView: NSView {
    let TEXTFIELD_MAX_LENGTH = 7
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        setupUI()
    }
    
    required init?(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        self.addSubview(tef)
        tef.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(44)
            make.top.equalToSuperview()
        }
        
        self.addSubview(formLab)
        formLab.snp.makeConstraints { (make) in
            make.top.equalTo(tef.snp.bottom).offset(5)
            make.width.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(16)
        }
        
        self.addSubview(canfirmBtn)
        canfirmBtn.snp.makeConstraints { (make) in
            make.width.equalTo(70)
            make.right.equalTo(0)
            make.height.equalTo(36)
            make.bottom.equalTo(0)
        }
        
        self.addSubview(cacalBtn)
        cacalBtn.snp.makeConstraints { (make) in
            make.width.equalTo(70)
            make.right.equalTo(canfirmBtn.snp.left).offset(-10)
            make.height.equalTo(36)
            make.bottom.equalTo(0)
        }
    }
    
    lazy var tef: InputTef = {
        let tef = InputTef.init(frame: NSRect.zero)
        tef.defaulTef.delegate = self
        tef.defaulTef.baseDelegate = self
        return tef
    }()
    
    lazy var formLab: NSTextField = {
        let view = NSTextField.label(title: "(仅支持中文，英文，数字，11位以内)".localized(), textColor: color_gray(200), font: NSFont.systemFont(ofSize: 12))
        view.alignment = .left
        return view
    }()
    
    lazy var canfirmBtn: LLCustomBtn = {
        let btn = LLCustomBtn()
        btn.defaultFont = NSFont.systemFont(ofSize: 16)
        btn.defaultTitleColor = NSColor.black
        btn.defaultBackgroundColor = color_gray(220)
        btn.defaultTitle = "确定".localized()
        btn.isHandCursor = true
        btn.action = #selector(canfirmBtnAction)
        btn.target = self
        btn.textAlignment = LLTextAlignmentCenter
        btn.zh_addBorder(radius: 10, borderColor: nil, borderWidth: 0)
        return btn
    }()
    
    lazy var cacalBtn: LLCustomBtn = {
        let btn = LLCustomBtn()
        btn.defaultFont = NSFont.systemFont(ofSize: 16)
        btn.defaultTitleColor = NSColor.black
        btn.defaultBackgroundColor = NSColor.white
        btn.defaultTitle = "取消".localized()
        btn.isHandCursor = true
        btn.action = #selector(cacalBtnAction)
        btn.target = self
        btn.textAlignment = LLTextAlignmentCenter
        btn.zh_addBorder(radius: 10, borderColor: color_gray(200), borderWidth: 0.5)
        return btn
    }()
    
    @objc func cacalBtnAction(senber:LLCustomBtn) {
        Dialog.shared.dismiss()
    }
    
    @objc func canfirmBtnAction(senber:LLCustomBtn) {
        let name = self.tef.defaulTef.stringValue
        if String.validatedNickName(nickNameStr: name) == true {
            Dialog.shared.dismiss()
            if let name = UserInfoManager.shared.ownid {
                NetEngine.reponseName(name, id: name) {
                    show_msg("设置成功，请重新开始会议".localized())
                }
            }
            
        }
    }
    
}
extension InputNameView:NSTextFieldDelegate,BaseTextFieldDelegate {
    func textFieldDidBecomeFirstResponder(senber: NSTextField) -> Bool {
        self.tef.isSelect = true
        return true
    }
    
    func textFieldDidResignFirstResponder(senber: NSTextField) -> Bool {
        self.tef.isSelect = false
        return true
    }
    
   
    
    func controlTextDidBeginEditing(_ obj: Notification) {
        self.tef.isSelect = true

    }
    
    func controlTextDidEndEditing(_ obj: Notification) {
        self.tef.isSelect = false
    }
    
    
    func controlTextDidChange(_ obj: Notification) {
        
        let tef = obj.object as? BaseTextField
        let strInput = tef?.stringValue
        let inputLength = strInput?.count
        if inputLength! > 0 {
            if inputLength! > TEXTFIELD_MAX_LENGTH {
                tef?.stringValue.substring(toIndex: TEXTFIELD_MAX_LENGTH)
                self.formLab.textColor = NSColor.red
                return
            }
            if String.validatedNickName(nickNameStr: strInput!) == true  {
                self.formLab.textColor = color_gray(200)
            }else {
                self.formLab.textColor = NSColor.red
            }
        }
        
        /*
        if inputLength! > 0 {
            if inputLength! > TEXTFIELD_MAX_LENGTH {
                tef?.stringValue.substring(toIndex: TEXTFIELD_MAX_LENGTH)
                return
            }
            let strTemp = NSMutableString.init(capacity: TEXTFIELD_MAX_LENGTH)
            for i in 0..<inputLength! {
                let temp = strInput![i]
                if temp>="0"&&temp<="9" {
                    strTemp.append(temp)
                }
            }
            tef?.stringValue = strTemp as String
        }
        */
    }
    
    
}

